package data;

import java.sql.PreparedStatement;
import java.sql.SQLException;

import android.app.Activity;

/**
 * This class sets an Account as admin
 * 
 * @author Stephen
 * 
 */
public class AccountSetAsAdmin extends DBBooleanTask<Account> {

	/** whether or not the account should be admin */
	private boolean isAdmin;

	/**
	 * Creates a set Account to admin class
	 * 
	 * @param activity
	 *            - the activity to show the loading bar
	 * @param data
	 *            - the account to set
	 * @param isAdmin
	 *            - if the account should be admin
	 * @throws DBInterfaceError
	 *             - the error to throw
	 */
	public AccountSetAsAdmin(Activity activity, Account data, boolean isAdmin)
			throws DBInterfaceError {
		super(activity, data);

		this.isAdmin = isAdmin;
	}

	/**
	 * The actual set account to admin code
	 * 
	 * @param data
	 *            - the Account to set to admin
	 */
	@Override
	protected boolean task(Account data) {
		boolean success = false;

		try {
			PreparedStatement setToAdmin = conn
					.prepareStatement("UPDATE account SET isAdmin=? WHERE accountName=?");
			setToAdmin.setBoolean(1, isAdmin);
			setToAdmin.setString(2, data.getAccountName());
			setToAdmin.execute();
			success = true;
		} catch (SQLException e) {
			success = false;
			e.printStackTrace();
		}

		return success;
	}
}
